package com.ymatou.operate.trading.api.testcase;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.operate.trading.api.parameter.OrderSettlementToSellerBean;
import com.ymatou.operate.trading.api.service.OrderSettlementToSellerCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.model.YmtAccountEntry;
import com.ymttest.database.sqlwapper.AccountDefaultWapper;
import com.ymttest.database.sqlwapper.DefaultWapper;
/**
*订单结算(给商家)接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_OrderSettlementToSeller{
	private static OrderSettlementToSellerBean ordersettlementtosellerBean;
	private static OrderSettlementToSellerCall ordersettlementtosellerCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单结算(给商家)接口");
	}

	@Before
	public void caseUp() {
		ordersettlementtosellerBean=new OrderSettlementToSellerBean();
		ordersettlementtosellerCall=new OrderSettlementToSellerCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(ordersettlementtosellerCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_OrderSettlementToSeller_001() {
		Logger.start(true,"订单结算(给商家)成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");		
			PlaceOrderResp placeOrderResp=new OrderCallService().createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId =placeOrderResp.getOrderList().get(0).getSellerId();
			int userId =placeOrderResp.getUserId();         
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice =  placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice, tradingId);			
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);			
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			// 确认收货
			new OrderCallService().confirmReceive(userId, orderId);			
			Logger.comment("前置数据准备结束");				
			//--SettlementStatus 结算状态 1---已结算  0--未结算
			List<Map> list=new DefaultWapper().selectYmtSettlementInvoice(orderId);
			Logger.comment("结算前");		
			Logger.verifyEquals(0, (Integer)list.get(0).get("SettlementStatus"), "验证状态为0未结算");
			Logger.verifyEquals("确认收货订单结算", (String)list.get(0).get("Remark"), "remark为确认收货订单结算");		
			ordersettlementtosellerBean.setOrderId(orderId);			
			ordersettlementtosellerCall.setData(ordersettlementtosellerBean);
			ordersettlementtosellerCall.callService();		
			Logger.comment("结算后");		
			List<Map> list2=new DefaultWapper().selectYmtSettlementInvoice(orderId);
			Logger.verifyEquals("ok", ordersettlementtosellerCall.getReturnData(), "验证接口返回结果");
			Logger.verifyEquals(1, (Integer)list2.get(0).get("SettlementStatus"), "验证状态为1已结算");
			Logger.verifyEquals("确认收货订单结算", (String)list2.get(0).get("Remark"), "remark为确认收货订单结算");
			//查询卖家资金流水表
			List<YmtAccountEntry> list3=new AccountDefaultWapper().selectAccountEntryByUserId(sellerId);		
			Logger.verifyEquals("订单收入", list3.get(0).getMemo(), "验证卖家资金流水表Memo");
			Logger.verifyEquals(totalPrice, list3.get(0).getAmount(), "验证卖家资金流水表Amount");	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_OrderSettlementToSeller_002() {
		Logger.start(true,"订单号不存在");
		try {
			ordersettlementtosellerBean.setOrderId(111);			
			ordersettlementtosellerCall.setData(ordersettlementtosellerBean);
			ordersettlementtosellerCall.callService();			
			Logger.verifyEquals("ok", ordersettlementtosellerCall.getReturnData(), "验证接口返回结果");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_OrderSettlementToSeller_003() {
		Logger.start(true,"订单为null");
		try {
			//ordersettlementtosellerBean.setOrderId();			
			ordersettlementtosellerCall.setData(ordersettlementtosellerBean);
			ordersettlementtosellerCall.callService();			
			Logger.verifyEquals("ok", ordersettlementtosellerCall.getReturnData(), "验证接口返回结果");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}